package com.api.controller;

import com.api.constant.CommonConstant;
import com.api.model.TMerchant;
import com.api.model.TMerchantDetail;
import com.api.model.TTag;
import com.api.service.MerchantDetailService;
import com.api.service.MerchantService;
import com.api.service.MerchantTagService;
import com.api.service.OrderService;
import com.api.utils.JsonReturnUtils;
import com.api.utils.OrderUtils;
import com.api.utils.RollPage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by PC on 2017/7/10.
 */
@RestController
@RequestMapping("api/merchant")
public class MerchantController {
    private static final Logger logger = Logger.getLogger(MerchantController.class);
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantTagService merchantTagService;
    @Autowired
    private MerchantDetailService merchantDetailService;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "list")
    public Object list(Integer type, Integer page, Integer pageSize, Double xCoordinate, Double yCoordinate) {
        page = (page == null || page == 0) ? 1 : page;
        pageSize = (pageSize == null || pageSize == 0) ? 5 : pageSize;
        OrderUtils order = new OrderUtils();
        order.pushDesc("create_time");
        Map<String, Object> params = new HashMap<String, Object>();
        if (xCoordinate != null && yCoordinate != null) {
            params.put("xCoordinate", xCoordinate);
            params.put("yCoordinate", yCoordinate);
        } else {
            params.put("xCoordinate", 121.431255);
            params.put("yCoordinate", 31.228723);
        }
        RollPage<TMerchant> rollPage = null;
        if (CommonConstant.MerchantType.Recommend.getValue().equals(type)) {
            rollPage = merchantService.getMerchantList(params, order, page, pageSize);
        } else if (CommonConstant.MerchantType.NearBy.getValue().equals(type)) {
            order = new OrderUtils();
            order.pushAsc("distance");
            rollPage = merchantService.getMerchantList(params, order, page, pageSize);
        } else {
            params.put("type", type);
            rollPage = merchantService.getMerchantList(params, order, page, pageSize);
        }
        List<TMerchant> merchantList = rollPage.getRecordList();
        setTagList(merchantList);
        return JsonReturnUtils.success(merchantList, page, rollPage.getPageSum(), rollPage.getiTotalRecords());
    }

    @RequestMapping(value = "bannerList")
    public Object bannerList(Integer id) {
        id = id == null ? 0 : id;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("merchantId", id);
        params.put("type", 1);
        params.put("status", CommonConstant.DataStatus.Normal.getValue());
        OrderUtils order = new OrderUtils();
        order.pushAsc("sort");
        List<TMerchantDetail> merchantDetailList = merchantDetailService.findListByParams(params, order);
        return JsonReturnUtils.success(merchantDetailList);
    }

    @RequestMapping(value = "detailList")
    public Object detailList(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("merchantId", id);
        params.put("type", 2);
        params.put("status", CommonConstant.DataStatus.Normal.getValue());
        OrderUtils order = new OrderUtils();
        order.pushAsc("sort");
        List<TMerchantDetail> merchantDetailList = merchantDetailService.findListByParams(params, order);
        return JsonReturnUtils.success(merchantDetailList);
    }

    private void setTagList(List<TMerchant> merchantList) {
        Map<String, Object> params;
        if (merchantList.size() > 0) {
            List<TTag> tagList;
            params = new HashMap<String, Object>();
            for (TMerchant merchant : merchantList) {
                float distance = Integer.valueOf(merchant.getDistance());
                if (distance % 1000 > 0) {
                    distance = distance / 1000;
                    distance = (float) (Math.round(distance * 100)) / 100;
                    merchant.setDistance(distance + "km");
                } else {
                    merchant.setDistance(distance + "m");
                }
                params.put("merchantId", merchant.getId());
                tagList = new ArrayList<TTag>();
                tagList = merchantTagService.getTagList(params);
                merchant.setTagList(tagList);
            }
        }
    }

    @RequestMapping(value = "detail")
    public Object detail(int id, Double xCoordinate, Double yCoordinate) {
        Map<String, Object> params = new HashMap<String, Object>();
        OrderUtils order = new OrderUtils();
        xCoordinate = 121.431255;
        yCoordinate = 31.228723;
        params.put("merchantId", id);
        params.put("xCoordinate", xCoordinate);
        params.put("yCoordinate", yCoordinate);
        TMerchant merchant = (TMerchant) merchantService.selectOneByParams(params);
        params = new HashMap<String, Object>();
        params.put("merchantId", merchant.getId());
        List<TTag> tagList = merchantTagService.getTagList(params);
        merchant.setTagList(tagList);
        params = new HashMap<String, Object>();
        params.put("merchantId", id);
        params.put("status", CommonConstant.OrderStatus.Comment.getValue());
        int commentCount = orderService.findCountByParams(params);
        Double score = orderService.selectAverageScoreByParams(params);
        merchant.setCommentCount(commentCount);
        score = score == null ? 0 : score;
        merchant.setCommentScore(score);
        return JsonReturnUtils.success(merchant);
    }

}
